package com.lingnan.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.dto.AddHrRequest;
import com.lingnan.dto.CompanyCreateDTO;
import com.lingnan.dto.UpdateCompanyRequest;
import com.lingnan.dto.UpdateHrDTO;
import com.lingnan.entity.po.Company;
import com.lingnan.entity.po.CompanyHr;
import com.lingnan.entity.vo.Result;
import com.lingnan.service.CompanyService;
import com.lingnan.utils.LoginContextHolder;
import com.lingnan.vo.CompanyVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/company")
@Slf4j
public class CompanyController {
    
    @Autowired
    private CompanyService companyService;

    /**
     * 创建企业信息
     */
    @PostMapping("/create")
    public Result<Company> createCompany(@Valid @RequestBody CompanyCreateDTO createDTO){
        try {
            Long userId = Long.valueOf(LoginContextHolder.getLoginId());

            if (userId == null) {
                return Result.fail("用户未登录");
            }

            Company company = companyService.createCompany(createDTO, userId);
            return Result.ok(company);

        } catch (RuntimeException e) {
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            return Result.fail("创建企业信息失败");
        }
    }


@DeleteMapping("/{companyId}")
public Result deleteCompany(@PathVariable("companyId") Long companyId) {
    try {
        boolean result = companyService.deleteCompany(companyId);
        return Result.ok(result ? "删除成功" : "删除失败");
    } catch (Exception e) {
        log.error("删除企业失败，企业ID: {}", companyId, e);
        return Result.fail("删除失败: " + e.getMessage());
    }
}

    /**
     * 修改企业信息
     */
    @PutMapping
    public Result updateCompany(@RequestBody(required = false) @Valid UpdateCompanyRequest request) {
        boolean result = companyService.updateCompany(request);
        return Result.ok(result ? "修改成功" : "修改失败");
    }

    /**
     * 根据ID查询企业详情
     */
    @GetMapping("/{companyId}")
    public Result getCompanyById(@PathVariable("companyId") Long companyId) {
        CompanyVO companyVO = companyService.getCompanyById(companyId);
        return Result.ok(companyVO);
    }

    /**
     * 分页查询企业列表
     */
    @GetMapping("/page")
    public Result getCompanyPage(
            @RequestParam(value = "current",defaultValue = "1") Integer current,
            @RequestParam(value = "size",defaultValue = "10") Integer size,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "status",required = false) Integer status) {

        Page<Company> page = new Page<>(current, size);
        IPage<CompanyVO> result = companyService.getCompanyPage(page, name, status);
        return Result.ok(result);
    }


    /**
     * 启用/禁用企业
     */
    @PostMapping("/toggle-status/{companyId}")
    public Result toggleCompanyStatus(
            @PathVariable("companyId") Long companyId,
            @RequestParam("status") Integer status) {

        boolean result = companyService.toggleCompanyStatus(companyId, status);
        return Result.ok(result ? "操作成功" : "操作失败");
    }

    /**
     * 根据用户ID查询企业信息
     */
    @GetMapping("/by-user/{userId}")
    public Result getCompanyByUserId(@PathVariable("userId") Long userId) {
        Company company = companyService.getCompanyByUserId(userId);
        return Result.ok(company);
    }
    
    /**
     * 添加HR
     */
    @PostMapping("/hr")
    public Result addHr(@RequestBody @Valid AddHrRequest request) {
        CompanyHr companyHr = new CompanyHr();
        companyHr.setCompanyId(request.getCompanyId());
        companyHr.setName(request.getName());
        companyHr.setPosition(request.getPosition());
        companyHr.setContactEmail(request.getContactEmail());
        companyHr.setContactPhone(request.getContactPhone());
        
        boolean result = companyService.addHr(companyHr, request.getPassword());
        return Result.ok(result ? "添加HR成功" : "添加HR失败");
    }
    
    /**
     * 删除HR
     */
    @DeleteMapping("/hr/{hrId}")
    public Result deleteHr(@PathVariable("hrId") Long hrId) {
        boolean result = companyService.deleteHr(hrId);
        return Result.ok(result ? "删除成功" : "删除失败");
    }
    
    /**
     * 修改HR信息，参数除了id，其他参数均可选传入
     */
    @PutMapping("/hr")
    public Result updateHr(@RequestBody @Valid UpdateHrDTO updateHrDTO) {
        boolean result = companyService.updateHr(updateHrDTO);
        return Result.ok(result ? "修改成功" : "修改失败");
    }
    
    /**
     * 查询企业HR列表
     */
    @GetMapping("/hr/list/{companyId}")
    public Result getHrList(@PathVariable("companyId") Long companyId) {
        List<CompanyHr> hrList = companyService.getHrList(companyId);
        return Result.ok(hrList);
    }
    
    /**
     * 根据ID查询HR
     */
    @GetMapping("/hr/{hrId}")
    public Result getHrById(@PathVariable("hrId") Long hrId) {
        CompanyHr companyHr = companyService.getHrById(hrId);
        if (companyHr == null){
            return Result.fail("HR记录不存在");
        }
        return Result.ok(companyHr);
    }

    //在创建企业里已经发送通知了，接口作废
//    /**
//     * 提交资质认证申请
//     */
//    @PostMapping("/qualification/{companyId}")
//    public Result submitQualification(@PathVariable("companyId") Long companyId) {
//        boolean result = companyService.submitQualification(companyId);
//        return Result.ok(result ? "提交成功，等待管理员审核" : "提交失败");
//    }
}

